#!/bin/bash

#author:xttcqw
#update:0815
#desc:install tengine of source
#usage:must have source code of openssl,pcre,tengine,zlib 
#usage2:parameter 1 is source code package dircetory

#check dir and source code
sourcePath=$1
checkDir(){
if [ ! -e $sourcePath ]
then
echo "文档${sourcePath}不存在"
exit
elif [ ! -d $sourcePath ]
then
echo "文档${sourcePath}不是文件夹"
exit
fi
}
#unpack the source code package

untar(){
cd $sourcePath
for i in `ls`
do
	suffix=${i##*.}
	if [ $suffix == "gz" ]
	then
		tar -zxf $i
	fi
done
unset suffix
}


conf_make_install(){
cd $sourcePath
#get directory info 
#dir_info is array for directory of path
confStr=" --with-pcre= --with-openssl= --with-zlib= "
for i in `ls`
do
        suffix=`file $i | grep 'directory'| wc -l `
        if [ $suffix -eq "1" ]
        then
              dirPath="$PWD/$i"
              dirName=${i%%-*}
              case "$dirName" in
              "pcre")
              confStr=${confStr/'pcre='/pcre=$dirPath}
              ;;
              "openssl")
              confStr=${confStr/'openssl='/openssl=$dirPath}
              ;;
              "zlib")
              confStr=${confStr/'zlib='/zlib=$dirPath}
              ;;
	      "tengine")
	      tenginePath=$dirPath
	      ;;
              esac
        fi
done
unset suffix
unset dirName
unset dirPath
#echo $confStr


#configure and make install
cd $tenginePath
touch 1.txt
./configure $confStr
make
make install

unset confStr
unset tenginePath
}

cpNginx(){
cd ~
cp ./set_nginx.sh /etc/init.d/nginx
chmod +x /etc/init.d/nginx
}

startNginx(){
/etc/init.d/nginx start &>/dev/null
}
checkDir
untar
conf_make_install
cpNginx
startNginx

unset sourcePath

